package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateCarrierRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchCarrierRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateCarrierRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusCarrierRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.CarrierApiResponse;
import com.wzdigit.wms.basic.client.response.basic.CarrierResponse;
import com.wzdigit.wms.basic.client.response.basic.ImportCarrierResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.CarrierService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 承运人 前端控制器
 * @author yangdongjie
 * @CarrierController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "承运人")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class CarrierController {
	
	@Autowired
    private CarrierService carrierService;

    private static final String REQUEST_MODEL = "/carrier";

	/**
	 * 承运人（下拉框）列表（启用状态）
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "承运人（下拉框）列表（启用状态）",notes = "承运人（下拉框）列表（启用状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/list")
    public List<CarrierApiResponse> list(
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return carrierService.list();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
    /**
     * 查询承运人列表
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @SuppressWarnings("rawtypes")
	@ApiOperation(value = "查询承运人列表",notes = "查询承运人列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchCarrier")
    public PagerInfo<List<CarrierResponse>> searchCarrier(
    		SearchCarrierRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return carrierService.searchCarrier(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 查询承运人
     * @param carrierId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询承运人",notes = "查询承运人")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getCarrier")
    public CarrierResponse getCarrier(
    		@ApiParam(value = "承运人ID", required = true) 
    		@RequestParam Integer carrierId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return carrierService.getCarrier(carrierId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 创建承运人
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建承运人",notes = "创建承运人")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createCarrier")
    public CarrierResponse createCarrier(
    		@RequestBody @Valid CreateCarrierRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return carrierService.createCarrier(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改承运人
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改承运人",notes = "修改承运人")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateCarrier")
    public CarrierResponse updateCarrier(
    		@RequestBody @Valid UpdateCarrierRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return carrierService.updateCarrier(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改承运人状态
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改承运人状态",notes = "修改承运人状态")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateStatus")
    public <T> Result<T> updateStatus(
    		@RequestBody @Valid UpdateStatusCarrierRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return carrierService.updateStatus(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 导入《承运人信息》
     * @param file
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入《承运人信息》", notes = "导入《承运人信息》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importCarrier")
    public ImportCarrierResponse importCarrier(MultipartFile file, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
			return carrierService.importCarrier(file, servletRequest, servletResponse);
        } catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
}
